﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RM_dll2;
using RM_dll2.Process;
using FinalTestMachine.Motion.Products;
using FinalTestMachine.Motion.IO;

namespace FinalTestMachine.Motion.Process.SubProcess
{
    internal class SubprocessUnloadNGProduct : ProcessIPauseBase
    {
        public static readonly SubprocessUnloadNGProduct Cur = new SubprocessUnloadNGProduct();

        /// <summary>
        /// 等待NG产品下料完成
        /// </summary>
        public bool IsWaitNGProductUnload { get; set; }

        private SubprocessUnloadNGProduct() : base("NG下料子流程", StaticInitial.Motion, true)
        {
        }

        public override void Stop()
        {
            base.Stop();
            IsWaitNGProductUnload = false;
        }

        protected override void ImplementRunAction()
        {
            string error = string.Empty;
            Ref_Step("开始NG下料流程");
            while (IsProcessRunning)
            {
                try
                {
                    WaitPause();
                    switch (Step)
                    {
                        case "开始NG下料流程":
                            if (!Funs.FunCommSelection.Cur.IsUseNGBox)
                            {
                                IsWaitNGProductUnload = false;
                                Ref_Step("NG下料流程结束");
                            }
                            else
                            {
                                IsWaitNGProductUnload = true;
                                Ref_Step("判断NG料盒是否已满");
                            }
                            break;

                        case "判断NG料盒是否已满":
                            /*if (!StaticIOHelper.In_NgBoxPutChecked.In())
                            {
                                WriteErrToUser("NG料盒已满请先清料");
                            }
                            else*/
                            WriteToUser("无NG料盒是否已满光电，跳过此步骤！");
                            {
                                Ref_Step("NG气缸缩回");
                            }
                            break;

                        case "NG气缸缩回":
                            IOScanHelper.NGisPut = false;
                            Ref_Step("判断NG料盒抽出");
                            break;

                        case "判断NG料盒抽出":
                            if (!StaticIOHelper.In_NgBoxChecked.In())
                            {
                                Ref_Step("判断NG料是否放入NG料盒");
                            }
                            break;

                        case "判断NG料是否放入NG料盒":

                            if (IOScanHelper.NGisPut)
                            {
                                IOScanHelper.NGisPut = false;
                                Ref_Step("等待NG盒归位");
                            }

                            break;

                        case "等待NG盒归位":
                            if (StaticIOHelper.In_NgBoxChecked.In())
                            {
                                IsWaitNGProductUnload = false;
                                ProductNumberManager.Cur.NgBoxNums++;

                                Ref_Step("NG下料流程结束");
                            }
                            break;

                        case "NG下料流程结束":
                            IsProcessRunning = false;
                            break;
                    }

                    Sleep(50);
                }
                catch (Exception ex)
                {
                    WriteErrToUser($"NG下料流程异常:{ex.Message}");
                }
            }
        }
    }
}